#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "cordic.h"

using namespace std;
// #define M_PI 3.1415926536897932384626

double abs_double(double var) {
    if (var < 0)
        var = -var;
    return var;
}

int main(int argc, char **argv) {

    FILE *fp;

    COS_SIN_TYPE s;    // sine
    COS_SIN_TYPE c;    // cos
    THETA_TYPE radian; // radian versuin of degree
    int has_errors = 0;

    char data_buf[256];
    char check_buf[256];

    fp = fopen("check.data", "r");
    assert(fp != NULL && "Couldn't open check data file");
    for (int i = 1; i < NUM_DEGREE; i++) {
        radian = i * M_PI / 180;
        cordic(radian, s, c);

        sprintf(data_buf, "%0.6lf", (double)s);
        int res = fscanf(fp, "%s", check_buf);
        assert(res == 1 && "read check data file failed");
        if (strcmp(data_buf, check_buf)) {
            printf("radian = %d, s = %s, expected_s = %s\n",
                   i,
                   data_buf,
                   check_buf);
            has_errors++;
        }
        sprintf(data_buf, "%0.6lf", (double)c);
        res = fscanf(fp, "%s", check_buf);
        assert(res == 1 && "read check data file failed");
        if (strcmp(data_buf, check_buf)) {
            printf("radian = %d, c = %s, expected_c = %s\n",
                   i,
                   data_buf,
                   check_buf);
            has_errors++;
        }
    }

    fclose(fp);


    if (has_errors) {
        fprintf(stderr, "Benchmark results are incorrect\n");
        return -1;
    }

    printf("Success.\n");
    return 0;
}
